<?php
/**
 * @ClassName: AgentLogic
 * @Description: TODO
 * @author: gydtrade
 * @Date: 2025-11-03 18:52
 * @Version: V1.0.0
 */

namespace app\common\logic;

use app\common\model;

class AgentLogic
{
    function checkAgent($member_id, $data, $upgrade_type = 0)
    {
        $agent = model\Agent::where('member_id', $member_id)->find();

        if ($agent) {
            // 代理存在，修改代理等级
            if (isset($data['change_agent_level']) && $data['change_agent_level'] != '') {
                $res = model\Agent::where('member_id', $member_id)->update([
                    'agent_type' => $data['agent_type'],
                    'agent_level' => $data['change_agent_level'],
                    'province' => $data['agent_province'],
                    'city' => $data['agent_city'],
                    'area' => $data['agent_area'],
                    'upgrade_type' => $upgrade_type,
                    'update_time' => date_now(),
                ]);
            }
        } else {
            // 代理不存在，添加代理
            if (isset($data['change_agent_level']) && $data['change_agent_level'] != '') {
                $arr = passCrypt(env('default_password'));

                $add_data = [
                    'username' => $data['mobile'],
                    'password' => $arr[0],
                    'member_id' => $member_id,
                    'agent_type' => $data['agent_type'],
                    'agent_level' => $data['change_agent_level'],
                    'province' => $data['agent_province'],
                    'city' => $data['agent_city'],
                    'area' => $data['agent_area'],
                    'salt' => $arr[1],
                    'super_pass' => passEncryp($arr[1], env('default_password')),
                    'upgrade_type' => $upgrade_type,
                    'update_time' => date_now(),
                ];
            }
        }
    }

}